/*============================================================================*/
/*                   CONTINENTAL AUTOMOTIVE SYSTEMS                           */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* name:            MAL_IOapi.h
* version:         1.0
* created_by:      Carlos Calvillo
* date_created:    May 17 2010
*=============================================================================*/
/* DESCRIPTION : Source file for basic IO functionality                       */
/*============================================================================*/
/* FUNCTION COMMENT : Provides APIs for basic handling of IOs                 */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 17/may/2010 |                               | Carlos Calvillo  */
/* Initial version for the Smart Car Race 2010 contest using Freescale        */
/* Bolero micro.                                                              */
/*============================================================================*/
#ifndef MAL_IOAPI_H                                 /* To avoid double inclusion */
#define MAL_IOAPI_H
/* Includes */
/* -------- */
#include "MCU_derivative.h"


/* PIN PCR values definitions */
#define PIN_PA0                     0
#define PIN_PA1                     1
#define PIN_PA2                     2
#define PIN_PA3                     3
#define PIN_PA4                     4
#define PIN_PA5                     5
#define PIN_PA6                     6
#define PIN_PA7                     7
#define PIN_PA8                     8
#define PIN_PA9                     9
#define PIN_PA10                        10
#define PIN_PA11                        11
#define PIN_PA12                        12
#define PIN_PA13                        13
#define PIN_PA14                        14
#define PIN_PA15                        15
#define PIN_PB0                     16
#define PIN_PB1                     17
#define PIN_PB2                     18
#define PIN_PB3                     19
#define PIN_PB4                     20
#define PIN_PB5                     21
#define PIN_PB6                     22
#define PIN_PB7                     23
#define PIN_PB8                     24
#define PIN_PB9                     25
#define PIN_PB10                        26
#define PIN_PB11                        27
#define PIN_PB12                        28
#define PIN_PB13                        29
#define PIN_PB14                        30
#define PIN_PB15                        31
#define PIN_PC0                     32
#define PIN_PC1                     33
#define PIN_PC2                     34
#define PIN_PC3                     35
#define PIN_PC4                     36
#define PIN_PC5                     37
#define PIN_PC6                     38
#define PIN_PC7                     39
#define PIN_PC8                     40
#define PIN_PC9                     41
#define PIN_PC10                        42
#define PIN_PC11                        43
#define PIN_PC12                        44
#define PIN_PC13                        45
#define PIN_PC14                        46
#define PIN_PC15                        47
#define PIN_PD0                     48
#define PIN_PD1                     49
#define PIN_PD2                     50
#define PIN_PD3                     51
#define PIN_PD4                     52
#define PIN_PD5                     53
#define PIN_PD6                     54
#define PIN_PD7                     55
#define PIN_PD8                     56
#define PIN_PD9                     57
#define PIN_PD10                        58
#define PIN_PD11                        59
#define PIN_PD12                        60
#define PIN_PD13                        61
#define PIN_PD14                        62
#define PIN_PD15                        63
#define PIN_PE0                     64
#define PIN_PE1                     65
#define PIN_PE2                     66
#define PIN_PE3                     67
#define PIN_PE4                     68
#define PIN_PE5                     69
#define PIN_PE6                     70
#define PIN_PE7                     71
#define PIN_PE8                     72
#define PIN_PE9                     73
#define PIN_PE10                        74
#define PIN_PE11                        75
#define PIN_PE12                        76
#define PIN_PE13                        77
#define PIN_PE14                        78
#define PIN_PE15                        79
#define PIN_PF0                     80
#define PIN_PF1                     81
#define PIN_PF2                     82
#define PIN_PF3                     83
#define PIN_PF4                     84
#define PIN_PF5                     85
#define PIN_PF6                     86
#define PIN_PF7                     87
#define PIN_PF8                     88
#define PIN_PF9                     89
#define PIN_PF10                        90
#define PIN_PF11                        91
#define PIN_PF12                        92
#define PIN_PF13                        93
#define PIN_PF14                        94
#define PIN_PF15                        95
#define PIN_PG0                     96
#define PIN_PG1                     97
#define PIN_PG2                     98
#define PIN_PG3                     99
#define PIN_PG4                     100
#define PIN_PG5                     101
#define PIN_PG6                     102
#define PIN_PG7                     103
#define PIN_PG8                     104
#define PIN_PG9                     105
#define PIN_PG10                        106
#define PIN_PG11                        107
#define PIN_PG12                        108
#define PIN_PG13                        109
#define PIN_PG14                        110
#define PIN_PG15                        111
#define PIN_PH0                     112
#define PIN_PH1                     113
#define PIN_PH2                     114
#define PIN_PH3                     115
#define PIN_PH4                     116
#define PIN_PH5                     117
#define PIN_PH6                     118
#define PIN_PH7                     119
#define PIN_PH8                     120
#define PIN_PH9                     121
#define PIN_PH10                        122

/* Pad Configuration Registers (PCR0...PCR122) */
/* ------------------------------------------- */

/* PA[1:0] Pad Output Assignment */
#define PA_GPIO                 (T_UWORD)(0x0000)               /* 00: Alternative Mode 0: GPIO */
#define PA_AM1                  (T_UWORD)(0x0400)               /* 01: Alternative Mode 1 */
#define PA_AM2                  (T_UWORD)(0x0800)               /* 10: Alternative Mode 2 */
#define PA_AM3                  (T_UWORD)(0x0C00)               /* 11: Alternative Mode 3 */


/* WPS: Weak Pull Up/Down Select */
#define WPS_PULL_UP                     1
#define WPS_PULL_DOWN           0

#define GPIO_INPUT                                      0
#define GPIO_OUTPUT                                     1

#define GPIO_OPEN_DRAIN_DISABLE         0
#define GPIO_OPEN_DRAIN_ENABLE          1

/* ========================================================================== *
 *                                    PIN APIs                                *
 * ========================================================================== */

/* PIN Configuration APIs */
/* ---------------------- */

/* >>>> Pin Function select */
/* Select Pin Function: configure it either as GPIO or an Alternate function */
#define IO_SetPinFctn(pinPcr,function)                  ( {SIU.PCR[(pinPcr)].R &= 0xF3FF; /* Clear bits. For function values see PA_GPIO, PA_AMx defines */ \
                                                                                   SIU.PCR[(pinPcr)].R |= ((function) & 0x0C00);} )     /* Set bits with new value */

/* >>>> Pin Direction */
/* Set Pin as Input (GPIO) */
#define IO_SetPinAsInput(pinPcr)                                ( SIU.PCR[(pinPcr)].B.IBE = TRUE )              /* Write bit IBE with 1 */
/* Set Pin as Output (GPIO) */
#define IO_SetPinAsOutput(pinPcr)                               ( SIU.PCR[(pinPcr)].B.OBE = TRUE )              /* Write bit OBE with 1 */
/* Set Pin as both Input and Output (GPIO) */
#define IO_SetPinAsInOut(pinPcr)                                ( {SIU.PCR[(pinPcr)].B.IBE = TRUE ); SIU.PCR[(pinPcr)].B.OBE = TRUE;} )         /* Write bits IBE and OBE with 1 */
/* Set port in tristate (Clear IBE and OBE bits) */
#define IO_SetPinDirReset(pinPcr)                               ( SIU.PCR[(pinPcr)].R &= 0xFCFF )               /* Clear bits IBE and ODE */

/* >>>> Pin Properties */
/* Pin Open Drain Output Enable  */
#define IO_PinOpenDrainEnable(pinPcr, value)    ( SIU.PCR[(pinPcr)].B.ODE = (value) )   /* 'value' options are TRUE or FALSE */
/* Pin Weak Pull Up/Down Enable */
#define IO_PinPullUpDownEn(pinPcr, value)               ( SIU.PCR[(pinPcr)].B.WPE = (value) )   /* 'value' options are TRUE or FALSE */
/* Weak Pull Up/Down Select */
#define IO_PinPullSelect(pinPcr, value)                 ( SIU.PCR[(pinPcr)].B.WPE = (value) )   /* See WPS register field defines for 'value' options  */

/* GPIO PIN Access (Read/Write) APIs */
/* --------------------------------- */

/* Read Pin (GPIO) */
#define IO_PinRead(pinPcr)                                              ( SIU.GPDI[(pinPcr)].B.PDI )
/* Write Pin (GPIO) */
#define IO_PinWrite(pinPcr, value)                              ( SIU.GPDO[(pinPcr)].B.PDO = (value) )

#endif